package no.awerup.armory_miner.collectors;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import no.awerup.armory_miner.AMClient;

import org.apache.camel.CamelContext;
import org.apache.camel.Processor;
import org.apache.commons.httpclient.HostConfiguration;
import org.apache.commons.httpclient.HttpClient;
import org.jboss.resteasy.client.ProxyFactory;
import org.jboss.resteasy.plugins.providers.RegisterBuiltin;
import org.jboss.resteasy.plugins.providers.jaxb.JAXBXmlRootElementProvider;
import org.jboss.resteasy.spi.ResteasyProviderFactory;

import com.thoughtworks.xstream.XStream;

import domain.Char;

public abstract class Collector implements Processor{
	CamelContext camelContext;	
	XStream xstream;
	

	public Collector(){
		this.xstream = new XStream();
		xstream.alias("char", Char.class);
	}
	
	public void setCamelContext(CamelContext camelContext) {
		this.camelContext = camelContext;				
	}

	AMClient createAMClient(){		    	
		RegisterBuiltin.register(ResteasyProviderFactory.getInstance());
        ResteasyProviderFactory.getInstance().addMessageBodyReader(JAXBXmlRootElementProvider.class);
        
        
        
        HttpClient httpClient = createHttpClient();
		return (AMClient) ProxyFactory.create(AMClient.class, "http://eu.wowarmory.com", httpClient  );		
	}

	private HttpClient createHttpClient() {
		HostConfiguration hostConf = new HostConfiguration();
        hostConf.setProxy( "proxy.spk.no", 8080);       
        
        HttpClient httpClient = new HttpClient();
        httpClient.getParams().setParameter("http.useragent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.2) Gecko/20070219 Firefox/2.0.0.2");
        httpClient.setHostConfiguration(hostConf);
		return httpClient;
	}
 
	protected DocumentBuilder createDocumentBuilder() throws ParserConfigurationException {
		DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
		domFactory.setNamespaceAware(true); // never forget this!
		DocumentBuilder builder = domFactory.newDocumentBuilder();
		return builder;
	}
}
